const Koa = require('koa')
const koa = new Koa()
const bodyParser = require('koa-bodyparser')
const proxy = require('koa2-proxy-middleware')
const views = require('koa-views')
const path = require('path')
const static = require('koa-static')

// 静态资源目录对于相对入口文件index.js的路径
const staticPath = './public'

koa.use(static(
  path.join( __dirname,  staticPath)
))

const options = {
  targets: {
    '/prj': {
      target: 'http://localhost:3000', // target host
      changeOrigin: true, // needed for virtual hosted sites
      pathRewrite: {
        '^/prj': ''
      }
    }
  }
}
 
koa.use(proxy(options))

// 使用ctx.body解析中间件
koa.use(bodyParser())

// 加载模板引擎
koa.use(views(path.join(__dirname, './view'), {
  extension: 'ejs'
}))

const Router = require('koa-router')
const authRouter = require('./router/auth')
const localRouter = require('./router/local') 

const router = new Router()
router.use('/api', authRouter.routes())
router.use('/local', localRouter.routes())

koa.use(router.routes())

// koa.use((ctx) => {
//   ctx.body = 'hello world.'
// })

koa.listen(3333, () => {
  console.log('localhost:3333')
})